<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML>
<html lang="en">
<head>
<base href="<%=basePath%>">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/main.css">
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/main.js"></script>
<title>代理商</title>


<style>
.main_div {
	min-width: 1000px
}

.pageChange {
	float: right;
	color: #337ab7;
}

.pageChange>span {
	cursor: pointer
}

.pageChange>span:hover {
	color: blue
}

.center {
	width: 200px;
	height: 100px;
	background-color: #ccc;
	position: absolute;
	left: 50%;
	top: 50%;
	margin-left: -100px;
	margin-top: -50px;
	z-index: 10;
	text-align: center;
	line-height: 50px;
	border-radius: 6px;
}
</style>
</head>
<body>
	<%@ include file="header.jsp"%>
	<%@ include file="left.jsp"%>
	<div id="index"
		style="position: absolute;left: 180px;top: 80px;bottom: 0;right: 0;">
		<section id="main">
			<div class="scrollbar1-1" id="style1-1">
				<div class="main_div" style="height:40px">
					<div class="main_title">
						<span>爆款管理</span>
					</div>
					<input style="display:none" value="5" id="pageCount"
						name="pageCount" />
					<button style="margin-top:20px" class="btn btn-primary"
						onclick="detailinfos()">添加爆款</button>
				</div>

				<div class="force-overflow1-1">
					<table id="showtable"
						class="table table-condensed table-bordered table-hover"
						style="text-align: center">
						<tr style="background-color: #59c7d2">
							<th style="text-align: center">序号</th>
							<th style="text-align: center">商品缩略图</th>
							<th style="text-align: center">爆款广告</th>
							<th style="text-align: center">商品名称</th>
							<th style="text-align: center">商品条码</th>
							<th style="text-align: center">爆款价格</th>
							<th style="text-align: center">修改</th>
							<th style="text-align: center">上下架</th>
							<th style="text-align: center">删除</th>
						</tr>
					</table>
					<div class="pageChange">
						<span onclick="prePage()">&nbsp;&nbsp;上一页&nbsp;&nbsp;</span> <span
							onclick="nextPage()">&nbsp;&nbsp;下一页&nbsp;&nbsp;</span> <em>
							&nbsp; 第&nbsp;<span id="pageNum">1</span>&nbsp;页&nbsp;&nbsp;
						</em> <em>/&nbsp;&nbsp;共&nbsp;<span id="totalpage"></span>&nbsp;页
						</em> <em>&nbsp;&nbsp;&nbsp;跳到第<input id="searchInput"
							style="width:46px" maxlength="4" type="text">页
						</em>
						<button class="btn btn-primary" onclick="searchPage()">搜索</button>
					</div>
				</div>
			</div>
		</section>
		<script>
			$(function() {
				selectinfo();
			})
			function selectinfo() {
				var barCode = $("#barCode").val();
				var pageNum = $("#pageNum").html();
				var pageCount = $("#pageCount").val();
				var productName = $("#productName").val();
				var chackaVal = $("#chakaVal").find("option:selected").val();
				var selectType2 = $("#selectType2").find("option:selected").val()
				if (productName == "") {
					productName = null;
				}
				if (barCode == "") {
					barCode = null;
				}
				if (!selectType2) {
					selectType2 = null
				}
				if (chackaVal == -1) {
					chackaVal = null
				}
				$.post(urls + "hotProductAction/getHotProduct",
					{
						agentId : localStorage.getItem("agentid"),
						goodsType : selectType2,
						barCode : barCode,
						pageNum : pageNum,
						pageCount : pageCount,
						productName : productName,
						checka : chackaVal
					},
					function(data) {
						//console.log(data)
						if (data) {
							var totalpage = data.totalpage;
							var orderlist = data.hotlist;
							var table = $("#showtable");
							$("tr").remove(".addtr");
							for (var i = 0; i < orderlist.length; i++) {
								var checka;
								var putaway;
								if (orderlist[i].checka == 0) {
									checka = "<input onclick='detailinfo(\"" +
										orderlist[i].productSimg +
										"|$" + orderlist[i].advertisement +
										"|$" + orderlist[i].barCode +
										"|$" + orderlist[i].productName +
										"|$" + orderlist[i].hotMoney +
										"|$" + orderlist[i].productDetail +
										"|$" + orderlist[i].id +
										"|$" + orderlist[i].goodstype +
										"|$" + orderlist[i].num +
										"|$" + orderlist[i].productBimg +
										"|$" + orderlist[i].hotDetailImg +
										"\")' type='button' class='btn btn-primary' value='修改' />"
								} else if (orderlist[i].checka == 1) {
									checka = "通过审核";
								} else if (orderlist[i].checka == 2) {
									checka = "<input onclick='detailinfo(\"" +
										orderlist[i].productSimg +
										"|$" + orderlist[i].advertisement +
										"|$" + orderlist[i].barCode +
										"|$" + orderlist[i].productName +
										"|$" + orderlist[i].hotMoney +
										"|$" + orderlist[i].productDetail +
										"|$" + orderlist[i].id +
										"|$" + orderlist[i].goodstype +
										"|$" + orderlist[i].num +
										"|$" + orderlist[i].productBimg +
										"|$" + orderlist[i].hotDetailImg +
										"\")' type='button' class='btn btn-primary' value='修改' />"
								}
								if(orderlist[i].putaway==0){
								putaway= "<input onclick='putawayfun(\""+1+"|$" + orderlist[i].id+"\")' type='button' class='btn btn-primary' value='已下架' />"
								}else if(orderlist[i].putaway==1){
                                putaway= "<input onclick='putawayfun(\""+0+"|$" + orderlist[i].id+"\")' type='button' class='btn btn-primary' value='已上架' />"
								}
		
								var append = "<tr id='" + orderlist[i].id + "'  class='addtr'><td style='line-height:120px'>" + (i + 1) + "</td>" +
									"<td ><img style='width:250px;height:120px' src='/hots/" + orderlist[i].productSimg + "' alt='加载中'/> </td>" +
									"<td ><img style='width:250px;height:120px' src='/hots/" + orderlist[i].advertisement + "' alt='加载中'/> </td>" +
									"<td style='line-height:120px'>" + orderlist[i].productName + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].barCode + "</td>" +
									"<td style='line-height:120px'>" + orderlist[i].hotMoney + "</td>" +
									"<td style='line-height:120px'>" + checka + "</td>" +
									"<td style='line-height:120px'>" + putaway + "</td>" +
									"<td style='line-height:120px'><input onclick='deletBurst(\"" + orderlist[i].id + "\")' type='button' class='btn btn-primary' value='删除' /></td></tr>";
								table.append(append);
							}
							$("#totalpage").html(totalpage);
						}
					}
					, "json"
				)
			}
			function deletBurst(e) { //删除爆款
				if (confirm("是够删除爆款？")) {
					$.post(urls + "hotProductAction/deleteHotProduct",
						{
							hotId : e
						},
						function(res) {
							if (res) {
								$("#" + e).css("display", "none")
							} else {
								alert("删除商品失败")
							}
						})
				}
			}
			function detailinfos(e) { //点击添加		
				$("#motai").css("display", "block");
				$("#main").css("display", "none");
			//var attr = e.split(" ");
			//$("#image").attr("src", attr[0]);
			//$("#productName2").val(attr[1]);
			//$("#productCard2").val(attr[2]);
			//$("#productPrice2").val(attr[3]);
			//$("#productDetail").val(attr[4])
			}
			function detailinfo(e) { //点击修改
				//console.log(e)
				$("#motai").css("display", "block");
				$("#main").css("display", "none");
				var attr = e.split("|$");
				$("#image").attr("src", "/hots/" + attr[0]);
				$("#image2").attr("src", "/hots/" + attr[1]);
				$("#barCode").val(attr[2]);
				$("#productName").val(attr[3]);
				$("#productPrice").val(attr[4]);
				$("#productDetail").val(attr[5]);
				$("#id").val(attr[6]);
				$("#numbers").val(attr[8]);
				$("#image4").attr("src", "/hots/" + attr[9]);
				$("#image3").attr("src", "/hots/" + attr[10]);
			}
		</script>
		<section id="motai" style="display:none">
			<div class="scrollbar1-1" id="style1-1">
				<div class="main_div" style="height: 560px;">
					<div class="main_title">
						<span>录入商品</span>
					</div>
					<form class="form-horizontal" enctype="multipart/form-data"
						style="margin-top: 30px" id="uploadForm">
						<div class="form-group">
							<label class="col-xs-2 control-label">商品分类：</label>
							<div class="col-xs-2">
								<select id="selectType1" class="col-xs-12"
									onclick="change1(this.value,'#selectType2');">
								</select> <input name="checka" value="0" style="display:none" />
							</div>
							<div class="col-xs-2">
								<select id="selectType2" class="col-xs-12" name="goodstype">
								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品名称：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="productName"
									name="productName" placeholder="商品名称"> <input name="id"
									type="text" style="display:none" id="id">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品条码：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="barCode"
									name="barCode" placeholder="商品条码">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品价格：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="productPrice"
									maxlength="5" name="hotMoney" placeholder="商品价格">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">默认超市库存：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="numbers"
									maxlength="5" name="num" placeholder="请输入默认每个超市的爆款库存">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">略缩图：</label>
							<div class="col-xs-3">
								<input type="file" id="picFile" name="Thumbnail"
									onchange="selectImage(this);" /> <img id="image"
									style="width:105px;height:105px" src="" />
							</div>
							<label class="col-xs-2 control-label"
								style="text-align: left;color: #ff6c29">*必须上传略缩图（建议大小为210*210像素）</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">banner广告：</label>
							<div class="col-xs-3">
								<input type="file" id="bannerFile" name="banner"
									onchange="selectImage2(this);"> <input type="text"
									id="agentId" name="agentId" style="display:none"> <img
									id="image2" style="width:375px;height:150px" src="" />
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传广告图片（建议大小为750*300像素）</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品图片：</label>
							<div class="col-xs-3">
								<input type="file" id="bigpicFile" name="bigpicFile"
									onchange="selectImage4(this);"> <img
									id="image4" style="width:375px;height:375px" src="" />
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传商品图片（建议大小为750*750像素）</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品详情图片：</label>
							<div class="col-xs-3">
								<input type="file" id="hotDetailFile" name="hotDetailFile"
									onchange="selectImage3(this);"> <img id="image3"
									style="width:310px;" src="" />
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传商品详情图片（建议宽度大小为750像素）</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品简介：</label>
							<div class="col-xs-4">
								<textarea class="col-xs-12" rows="5" id="productDetail"
									name="productDetail"></textarea>
							</div>

						</div>

						<div class="col-xs-4 col-xs-offset-2">
							<button id="setButton" class="col-xs-4 btn btn-primary"
								type="button">提交审核</button>
						</div>

					</form>
				</div>
			</div>
		</section>
	</div>
	<script>
		function putawayfun(e){
		var attr = e.split("|$");
		var hotId =attr[1];
		var putaway =attr[0];
		   $.post(urls+"hotProductAction/putawayHotProduct",
					{
						putaway : putaway,
						hotId : hotId,
					},
					function(data) {
					if(data=="false"){
					alert("操作失败");
					   }
					   selectinfo();
					});
		}
		var image = '';
		function selectImage(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage2(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image2').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage3(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image3').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage4(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image4').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		$(function() { //ajax异步提交表单，好东西
			$("#agentId").val(localStorage.getItem("agentid"))
			$("#setButton").click(function() {
				if ($("#id").val()) { //判断有id则是修改，无则是新加
					if ($("#selectType2").find("option").val()) {
						if ($("#productName").val()) {
							if ($("#barCode").val()) {
								if ($("#productPrice").val()) {
									if ($("#productDetail").val()) {
										/********************用于判断图片大小以及图片名称是否合法******************/
										var aarr = document.getElementsByTagName("input");
										var barr = [];
										var picreg = /^[A-Za-z0-9\._]{1,100}$/; //图片名称正则
										var flag1 = false; //用于判断图片大小
										var flag2 = false; //用于判断图片名称
										for (var i = 0; i < aarr.length; i++) {
											if (aarr[i].type == "file") {
												barr.push(aarr[i])
											}
										}
										for (var z = 0; z < barr.length; z++) {
											if (barr[z].files.length == 0) {
											} else {
												if (barr[z].files[0].size > 204800) { //判断图片大小是否大于200kb
													flag1 = true;
												}
												if (!picreg.test(barr[z].files[0].name)) { //判断图片大小是否大于200kb
	
													flag2 = true;
												}
											}
										}
										if (flag1) {
											alert("上传图片不得超过200kb每张");
											return;
										}
										if (flag2) {
											alert("图片名称只能由字母与数字组成");
											return;
										}
										/******************用于判断图片大小以及图片名称是否合法****************/
										$.ajax({
											url : urls + "hotProductAction/updateHotProduct",
											type : 'POST',
											cache : false,
											data : new FormData($('#uploadForm')[0]),
											processData : false,
											contentType : false
										}).done(function(res) {
											if (res) {
												alert("提交成功")
												$("#motai").css("display", "none");
												$("#main").css("display", "block");
												selectinfo();
											} else {
												alert("提交失败,商品已审核或服务器异常");
												/* $("#motai").css("display", "none");
												$("#main").css("display", "block"); */
												selectinfo();
											}
										}).fail(function(res) {});
									} else {
										alert("请输入商品详情")
									}
								} else {
									alert("请输入商品价格")
								}
							} else {
								alert("请输入商品条码")
							}
						} else {
							alert("请输入商品名称")
						}
					} else {
						alert("请选择商品分类")
					}
				} else {
				if(!$("#bigpicFile").val()){
				alert("请添加商品图片!")
				return;
				}
					if ($("#selectType2").find("option").val()) {
						if ($("#productName").val()) {
							if ($("#barCode").val()) {
								if ($("#productPrice").val()) {
									if ($("#picFile").val()) {
									    
										if ($("#bannerFile").val() && $("#hotDetailFile").val()) {
											if ($("#productDetail").val()) {
												/********************用于判断图片大小以及图片名称是否合法******************/
												var aarr = document.getElementsByTagName("input");
												var barr = [];
												var picreg = /^[A-Za-z0-9\.\_]{1,100}$/; //图片名称正则
												var flag1 = false; //用于判断图片大小
												var flag2 = false; //用于判断图片名称
												for (var i = 0; i < aarr.length; i++) {
													if (aarr[i].type == "file") {
														barr.push(aarr[i])
													}
												}
												for (var z = 0; z < barr.length; z++) {
													if (barr[z].files[0].size > 200000) { //判断图片大小是否大于200kb
														flag1 = true;
													}
													if (!picreg.test(barr[z].files[0].name)) { //判断图片大小是否大于200kb
														flag2 = true;
													}
												}
												if (flag1) {
													alert("上传图片不得超过200kb每张");
													return;
												}
												if (flag2) {
													alert("图片名称只能由字母与数字组成");
													return;
												}
												/******************用于判断图片大小以及图片名称是否合法****************/
												$.ajax({
													url : urls + "hotProductAction/setHotProduct",
													type : 'POST',
													cache : false,
													data : new FormData($('#uploadForm')[0]),
													processData : false,
													contentType : false
												}).done(function(res) {
													if (res) {
														alert("提交成功")
														$("#motai").css("display", "none");
														$("#main").css("display", "block");
														selectinfo()
													} else {
														alert("提交失败")
													}
												}).fail(function(res) {});
											} else {
												alert("请输入商品详情")
											}
										} else {
											alert("请输入商品轮播图")
										}
									} else {
										alert("请输入商品略缩图")
									}
								} else {
									alert("请输入商品价格")
								}
							} else {
								alert("请输入商品条码")
							}
						} else {
							alert("请输入商品名称")
						}
					} else {
						alert("请选择商品分类")
					}
	
				}
	
			})
	
		})
	</script>
</body>
</html>
